/*
* @Author: root
* @Date:   2017-11-18 21:31:01
* @Last Modified by:   root
* @Last Modified time: 2017-11-18 22:42:07
*/
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide         = require('page/common/nav-side/index.js');
var _utils          = require('util/util.js');
var _user           = require('service/user-service.js');

var page = {
	init : function(){
		this.onload();

		this.bindEvent();
	},
	onload  : function(){
		//初始化左侧菜单
		navSide.init({
		name:'user-pass-update'
		});
	},
	bindEvent : function(){
		var _this = this;
		$(document).on('click','.btn-submit',function(){
			var userInfo = {
				password         : $.trim($("#password").val()),
				passwordNew 	 : $.trim($("#password-new").val()),
				passwordConfirm  : $.trim($("#password-confirm").val())
			},
				validateResult = _this.validateForm(userInfo);

			if(validateResult.status){
				//更改用户密码
				_user.updatePassword({
					passwordOld : userInfo.password,
					passwordNew : userInfo.passwordNew
				},function(res,msg){
 				_utils.successTips(msg);
 				//修改成功后退出用户
 				 _user.logout(function(res){
                window.location.href = './user-login.html';
                 alert("请重新登陆");
            }, function(errMsg){
                util.errorTips(errMsg);
            });
 				

				},function(errMsg){
					_utils.errorTips(validateResult.msg);
				});
			}
			else {
				_utils.errorTips(validateResult.msg);
			}
		});
	},//验证字段信息
	validateForm : function(formData){
		
		var result = {
			status : false,
			msg    : ''
		};
		//验证原密码是否为空
		if(!_utils.validate(formData.password, 'require')){
            result.msg = '原密码不能为空';
            return result;
        }
        if (!formData.passwordNew||formData.passwordNew.length <6) {
			result.msg = '新密码长度不得少于6位';
			return result;
		}
		if (!formData.password||formData.password.length <6) {
			result.msg = '密码长度不得少于6位';
			return result;
		}
		if(!_utils.validate(formData.passwordConfirm, 'require')){
            result.msg = '请确认输入密码';
            return result;
        }
		//问题不能为空
		if (formData.passwordNew !== formData.passwordConfirm){
			result.msg = '两次输入密码不正确';
		}

		//通过验证，返回正确提示
		result.status = true;
		result.msg    = '验证通过';
		return result;
	}
};
$(function(){
    page.init();
});